package com.cxg.fairy.ec.detail;

import android.graphics.Paint;
import android.os.Bundle;
import android.renderscript.Double2;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.ContentFrameLayout;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.util.log.LatteLogger;
import com.cxg.fairy.util.money.MoneyUtil;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;
import com.cxg.fairy.delegates.LatteDelegate;
import com.hedgehog.ratingbar.RatingBar;
import com.joanzapata.iconify.widget.IconTextView;

import butterknife.BindView;
import butterknife.OnClick;
import me.gujun.android.taggroup.TagGroup;

/**
 * Created by CFairy
 */

public class GoodsInfoDelegate extends LatteDelegate {

    @BindView(R2.id.tv_goods_info_title)
    AppCompatTextView mGoodsInfoTitle = null;
    @BindView(R2.id.tv_goods_info_desc)
    AppCompatTextView mGoodsInfoDesc = null;
    @BindView(R2.id.tv_goods_info_price)
    AppCompatTextView mGoodsInfoPrice = null;
    @BindView(R2.id.tv_goods_info_unit)
    AppCompatTextView mGoodsInfoUnit = null;
    @BindView(R2.id.tv_goods_info_real_unit)
    AppCompatTextView mGoodsInfoRealUnit = null;
    @BindView(R2.id.tv_goods_info_default_price)
    AppCompatTextView mGoodsInfoDefaultPrice = null;
    @BindView(R2.id.tv_goods_info_default_price_tag)
    AppCompatTextView mGoodsInfoDefaultPriceTag = null;
    @BindView(R2.id.rst_goods_detailcomment_start)
    RatingBar mRatingBar = null;

    @BindView(R2.id.frame_goods_comment_header)
    LinearLayoutCompat mFrameCommentHeader = null;

    //清单
    @BindView(R2.id.itv_add_favor_list)
    IconTextView mIconAddFavor = null;
    @BindView(R2.id.tv_add_favor_list)
    AppCompatTextView tvAddFavor = null;

    private static final String ARG_GOODS_DATA = "ARG_GOODS_DATA";
    private JSONObject mData = null;
    @Override
    public Object setLayout() {
        return R.layout.delegate_goods_info;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        final String goodsData = args.getString(ARG_GOODS_DATA);
        mData = JSON.parseObject(goodsData);
    }

    public static GoodsInfoDelegate create(String goodsInfo) {
        final Bundle args = new Bundle();
        args.putString(ARG_GOODS_DATA, goodsInfo);
        final GoodsInfoDelegate delegate = new GoodsInfoDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        final String name = mData.getString("name");
        final String desc = mData.getString("description");
        final String price = MoneyUtil.formatMoney(mData.getDouble("price"));
        final String unit = mData.getString("unit");
        final String defaultPrice = MoneyUtil.formatMoney(mData.getDouble("marketPrice"));


        mRatingBar.setStar(5.0f);
        mGoodsInfoUnit.setText("/"+unit);
        mGoodsInfoRealUnit.setText("/"+unit);
        mGoodsInfoDefaultPrice.setText(defaultPrice);
        mGoodsInfoDefaultPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        mGoodsInfoDefaultPriceTag.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

//        mTagGroup.setTags(new String[]{"每袋5斤", "每袋2斤", "每袋10斤"});
        mGoodsInfoTitle.setText(name);
        mGoodsInfoDesc.setText(desc);
        mGoodsInfoPrice.setText(price);

        initGoodsCommentList(null);

        mFrameCommentHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
    }



    private void initGoodsCommentList(JSONObject data) {
        getSupportDelegate().loadRootFragment(R.id.frame_goods_comment, CommentListDelegate.create(0,100));
    }

}
